﻿using System.Collections.Generic;
using System.Linq;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;

namespace HappyBa.Core.Validators
{
    public class DevShouldHaveValidInDevTasks : IValidator
    {
        private const int MaxTasksCount = 2;
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            foreach (var u in context.Metrics.Users.Where(context.Metrics.IsDeveloper).Where(x => context.ActiveEmployees.Contains(x.ToString())))
            {
                var tasks = context.Metrics.EstimatableEntities
                    .Where(x=>x.Assignments.Any(y=>y.GeneralUser.Id==u.Id))
                    .Where(x => !(x is UIUserStory))
                    .Where(context.Metrics.IsInProgress)
                    .ToArray();
                if (tasks.Length > MaxTasksCount)
                {
                    yield return new ValidationResult(
                        string.Format("Developer {0} have more than {1} tasks or bugs in development: {2}", u, MaxTasksCount, string.Join(", ", tasks.Select(x => x.Id))))
                    {
                        User = u
                    };
                }
                else if (tasks.Length == 0)
                {
                    yield return new ValidationResult(
                        string.Format("Developer {0} don't have any tasks or bugs", u))
                    {
                        User = u
                    };
                }
            }
        }
    }
}
